% This file creates Figure 11 in the paper, presenting the response to
% productivity to a one unit productivity shock. 

close all; clear; clc;

global P2 lambda

%% Options: 

graph           =   1;          % = 1 to generate a plot. 

csv_file        =   1;          % = 1 if you want to import from CSV.
                                % = 0 if you want to import from XLSX.
                                
use_data        =   1;          % = 1   if baseline data

%% Importing and Preparing the data.

if csv_file == 1
    data            =   csvread('Data_File_Baseline.csv');
    % UM Expectations Median:     
    pilev_data      =   csvread('Data_File_Inflation.csv');
    pilev           =   pilev_data(:,6);     
else
    data            =   xlsread('Data_File.xlsx','Baseline');
    % UM Expectations Median:
    pilev           =   xlsread('Data_File.xlsx','Inflation','F2:F281');
end

% Import the specific variables. 
dz          =   data(:,3);
dy          =   data(:,4);
zlb         =   data(:,5);
rec         =   data(:,6);
zlev        =   data(:,7);
ylev        =   data(:,8);                 % Output is GDP
time        =   data(:,9);
[T,n]       =   size(dy);

% Defining the sample:
st          =   148;                        % = 1 for full sample 
                                            % = 148 for 1984Q1 
                                            % = 52 for 1960Q1. 
                                                                                        
%% Options for local projections.

P           =   2;      % Number of lags for endogenous variables.
H           =   9;      % Maximum number of leads.
h1          =   1;      % For splines.        
lambda      =   100;    % Value of penalty of SLP.
r           =   3;      % r-1 = order of the limit polynomial for the SLP.

%% Creating variables.

% LHS:
ylev2       =   ylev(st:end);
% Productivity:
zlev2       =   zlev(st:end);
% Inflation:
pilev2      =   pilev(st:end);
% Dummy for ZLB:
zlb2        =   zlb(st:end);

% Creating W matrix: 
ff          =   [ylev2 zlev2];
w           =   lagmatrix(ff,1:P) ;     % Remove period t observations.

% Creating lagged variables interacted with period-t ZLB dummy:
[TY,YT]     =   size(w);
zlb3        =   zeros(TY,YT);
for jx = 1:YT
    zlb3(:,jx)  =   zlb2;
end
www         =   zlb3.*w; 
zlb4        =   1-zlb3;
wwww    	=   zlb4.*w;

%% Creating the IRFs.

% LHS variable:    
y                       =   zlev2(1+P:end,:); 

% Local projection.
        % Standard case.   
w                       =   [zlb2(1+P:end) zlb2(1+P:end).*zlev2(1+P:end),...
                            wwww(1+P:end,:), www(1+P:end,:)];
x                       =   (1-zlb2(1+P:end)).*zlev2(1+P:end,:); % Shock variable.
lp                      =   locproj_var(y,x,w,h1,H,'reg'); 
% Extract the IRF from local projection: 
irf                     =   lp.IR(2:H+1); 
% Construct standard errors:
u                       =   lp.Y - lp.X*lp.theta;
[nwse_lp,vc_lp]         =   NeweyWest(u,lp.X,0,0);
    % ZLB case.
w                       =   [zlb2(1+P:end) (1-zlb2(1+P:end)).*zlev2(1+P:end),...
                            wwww(1+P:end,:), www(1+P:end,:)];
x                       =   zlb2(1+P:end).*zlev2(1+P:end,:); 
lp_zlb                  =   locproj_var(y,x,w,h1,H,'reg'); 
% Extract the IRF from local projection: 
irf_zlb                 =   lp_zlb.IR(2:H+1);  
% Construct standard errors:
u                       =   lp_zlb.Y - lp_zlb.X*lp_zlb.theta;
[nwse_lp_zlb,vc_lp_zlb] =   NeweyWest(u,lp_zlb.X,0,0);

% Smooth local projection.
        % Standard case.  
w                       =   [zlb2(1+P:end) zlb2(1+P:end).*zlev2(1+P:end),...
                             wwww(1+P:end,:), www(1+P:end,:)];
x                       =   (1-zlb2(1+P:end)).*zlev2(1+P:end,:); 
slp                     =   locproj_var(y,x,w,h1,H,'smooth',r,lambda); 
% Extract the IRF from local projection: 
irf_slp                 =   slp.IR(2:H+1);
P2                      =   slp.P2;
% Construct standard errors:
u                       =   slp.Y - slp.X*slp.theta;
[nwse_slp,vc_slp,Q]     =   NeweyWest_slp(u,slp.X,0,0);
V2                      =   slp.B*vc_slp(1:slp.K,1:slp.K)*slp.B';
se_slp                  =   sqrt(diag(V2));
        % ZLB case.
w                       =   [zlb2(1+P:end) (1-zlb2(1+P:end)).*zlev2(1+P:end),...
                             wwww(1+P:end,:), www(1+P:end,:)];
x                       =   zlb2(1+P:end).*zlev2(1+P:end,:); 
slp_zlb                 =   locproj_var(y,x,w,h1,H,'smooth',r,lambda); 
% Extract the IRF from local projection: 
irf_slp_zlb             =   slp_zlb.IR(2:H+1);
% Construct standard errors:
u                       =   slp_zlb.Y - slp_zlb.X*slp_zlb.theta;
[nwse_slp_zlb,vc_slp_zlb] = NeweyWest_slp(u,slp_zlb.X,0,0);
V22                     =   slp_zlb.B*vc_slp_zlb(1:slp_zlb.K,1:slp_zlb.K)*slp_zlb.B';
se_slp_zlb              =   sqrt(diag(V22));

%% Creating graph. 
 if graph == 1
     
    t                           =   0:H-1;
    p                           =   0.9;
    bb                          =   tinv(p,TY-P);

    figure
    plot(t,irf_slp,'x-b',t,irf_slp_zlb,'-r','Linewidth',1.5);
    h = legend('No ZLB','ZLB','Location','SouthWest');
    set(h,'Interpreter','latex');
    hold on
    shadedplot(t,(irf_slp+bb*se_slp(1:H))',(irf_slp-bb*se_slp(1:H))',[0.75,0.75,0.75],'w');
    hold on
    plot(t,irf_slp,'x-b',t,irf_slp_zlb,'-r',t,irf_slp_zlb+bb*se_slp_zlb(1:H),'*--r',t,irf_slp_zlb-bb*se_slp_zlb(1:H),'*--r','Linewidth',1.5);
    grid on
    title('Productivity to Productivity','Interpreter','latex','fontSize',14);
    xlabel('Horizon','Interpreter','latex','fontSize',12);
    ylabel('Percent','Interpreter','latex','fontSize',12);
    
 end 
